<?php
class Basededonnees extends mysqli {
	private $conf;
	
	public function __construct(Configuration $cfg) {
		parent::__construct($cfg->getserveur(),
								$cfg->getutilisateur(),
								$cfg->getmotdepasse(),
								$cfg->getbasededonnees());
		$this->conf=$cfg;
	}
	
	public function query($query, $resultmode=NULL) {
	//print_r($query);
		if($this->conf->getdebug())
			echo "<p class=\"test\">".$query."</p>\n";
		$result=parent::query($query,$resultmode);
		if($result===FALSE && $this->conf->getdebug()) {
			echo "<p class=\"notif\">".$this->errno
					.": ".$this->error."</p>\n";
		}
		return $result;
	}
	
	public function ecrire(Enregistrement $e) {
		$sql="insert into ".$e->gettable();
		$fields="";
		$values="";
		
		foreach ($e->getleschamps() as $nom=>$champs) {
			if ($champs->isnull())
				continue;
			if(strlen($fields)>0) {
				$fields.=", ";
				$values.=", ";
			}
			$fields.=$nom;
			$values.=$champs->valeurSQL();
		}
		
		$sql.=" (".$fields.") values (".$values.")";
		//echo $sql;
		if(!$this->query($sql)) {
			return false;
		}
		
		$e->setmodifie(false); //On note que l'objet a ete enregistre
		if(!$result=$this->query("select last_insert_id()"))
			return false;
		$ligne=$result->fetch_array();
		$rc=intval($ligne["last_insert_id()"]);
		$result->close();
		return $rc;
	}
	
	public function maj(Enregistrement $e) {
		$modif="";
		foreach ($e->getleschamps() as $nom=>$champs) {
			if(!$champs->iscle() && $champs->ismodifie()) {
				if(strlen($modif)>0) 
					$modif.=", ";
				$modif.=$nom."=".$champs->valeurSQL();
			}
		}
		$sql="update " . $e->gettable() . " set " . $modif . " where " . $e->getcle()
			. "=". $e->getchampscle()->getvaleur();
			
		if(!$this->query($sql)) {
			return false;
		}
		$e->setmodifie(false); //On note que l'objet a ete enregistre
		return true;
	}
	
	public function rechercher(Enregistrement $e, $sql="") {
		if($sql=="") {
			$cherche="";
			foreach ($e->getleschamps() as $nom=>$champs) {
				if(!$champs->isnull() && $champs->isrecherche()) {
					if(strlen($cherche)>0)
						$cherche.=" and ";
					$cherche.=$nom."=".$champs->valeurSQL();
				}
			}
			if ($cherche != ""){
				$sql= "select * from " . $e->gettable() . " where " . $cherche . " order by " . $e->getcle(). " desc";
			}else{
				$sql= "select * from " . $e->gettable() . " order by " . $e->getcle(). " desc";
			}
		}		
		//echo $sql;
		if(!$result=$this->query($sql)) {
			return false;
		}
		$rc=array();
		$fields=$result->fetch_fields();
		
		while ($ligne=$result->fetch_array(MYSQLI_ASSOC)) {
			$e=clone $e;
			foreach ($ligne as $name=>$value) {
				$field=current($fields);
				if($field != null){
				
					$ancien_champs=$e->getunchamps($name);
					$c=clone $ancien_champs;
					$type="";
					switch ($field->type) {
						case 253: case 254: $type="chaine"; $c->setchaine($value); break;
						case 3: $type="nombre"; $c->setnombre($value); break;
						case 10: case 12: $type="date"; $c->setdate($value); break;
						case 1: $type="booleen"; $c->setbooleen($value); break;
					}
					$e->setchamps($c);
				}
				$field=next($fields);
				
			}
			$rc[]=$e;
			reset($fields);
		}
		$result->close();
		return $rc;
	}

	public function supprimer(Enregistrement $e) {
		$sql="delete from ".$e->gettable()." where ".$e->getcle()
			."=".$e->getchampscle()->getvaleur();
		if(!$this->query($sql)) {
			return false;
		}
		$e->setmodifie(false); //On note que l'objet a ete "enregistre"
		return TRUE;
	}
	
	public function liredonnee(Enregistrement $e, $id) {
		$c=clone $e->getchampscle();
		$c->setvaleur($id);
		$c->setrecherche(true);
		$e->setchamps($c);
		$result=$this->rechercher($e);
		if (count($result)==0)
			return false;
		else
			return $result[0];
		
	}
	
	public function lirelieu($id) {
		$l=new Lieu($this->conf,$this);
		return $this->liredonnee($l, $id);
	}
	
	public function lireoperateur($id) {
		$o=new Operateur($this->conf,$this);
		return $this->liredonnee($o, $id);
	}
	
	public function liresession($id) {
		$s=new Session($this->conf,$this);
		return $this->liredonnee($s, $id);
	}
	
	public function lirequestion($id) {
		$q=new Question($this->conf,$this);
		return $this->liredonnee($q, $id);
	}
	
	public function lireepreuve($id) {
		$e=new Epreuve($this->conf,$this);
		return $this->liredonnee($e, $id);
	}
	
	public function lireexamen($id) {
		$e=new Examen($this->conf,$this);
		return $this->liredonnee($e, $id);
	}
	
	public function liresoustheme($id) {
		$s=new sousTheme($this->conf,$this);
		return $this->liredonnee($s, $id);
	}
	
	public function liretheme($id) {
		$t=new Theme($this->conf,$this);
		return $this->liredonnee($t, $id);
	}
	
	public function lirecomprend($id) {
		$c=new Comprend($this->conf,$this);
		return $this->liredonnee($c, $id);
	}
	
	public function liresederoule($id) {
		$s=new Sederoule($this->conf,$this);
		return $this->liredonnee($s, $id);
	}
	
	public function liresecompose($id) {
		$s=new Secompose($this->conf,$this);
		return $this->liredonnee($s, $id);
	}
	
	function __destruct() {
		$this->close();
	}
}
?>